local prefab_id = "webbert_seedbox"
local assets_id = "webbert_seedbox"

local assets =
{
    Asset( "ANIM", "anim/"..assets_id..".zip"),
    Asset( "ATLAS", "images/inventoryimages/"..assets_id..".xml" ),
}

local prefabs =
{
}

local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddLight()
    inst.entity:AddMiniMapEntity()
    inst.entity:AddNetwork()
    inst.entity:AddSoundEmitter()
    MakeInventoryPhysics(inst)

    inst.AnimState:SetBank(assets_id)
    inst.AnimState:SetBuild(assets_id)
    inst.AnimState:PlayAnimation("idle",true)

    MakeInventoryFloatable(inst, "med", nil, 0.75)

    inst.Light:SetFalloff(0.5)
    inst.Light:SetIntensity(.6)
    inst.Light:SetRadius(1)
    inst.Light:SetColour(92/255, 249/255, 255/255)
    inst.Light:Enable(true)

    inst.entity:SetPristine()

    inst:AddTag("nosteal")

    -- inst.MiniMapEntity:SetIcon(prefab_id..".tex")

    ---@cast inst ent_webbert_seedbox

    if not TheWorld.ismastersim then
        return inst
    end

    ---@class ent
    ---@field webbert_seedbox_keepitem boolean|nil # 真空种子匣: 是否保留物品

    inst.webbert_seedbox_keepitem = false

    -- inst:AddComponent("talker")
    inst:AddComponent("inspectable")

    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem.imagename = assets_id
    inst.components.inventoryitem.atlasname = "images/inventoryimages/"..assets_id..".xml"
    inst.components.inventoryitem:SetOnDroppedFn(function()
        inst.Light:Enable(true)
    end)
    inst.components.inventoryitem:SetOnPutInInventoryFn(function()
        inst.Light:Enable(false)
    end)

    inst:AddComponent('webbert_seedbox')

    inst:AddComponent('container')
    inst.components.container:WidgetSetup(prefab_id)

    -- inst:ListenForEvent('onopen',function (this, data)
    --     inst.AnimState:PlayAnimation('open',true)
    --     inst.components.inventoryitem:ChangeImageName("webbert_pack_open")

    --     inst.AnimState:SetSymbolLightOverride('thumbnail',1)
    -- end)
    -- inst:ListenForEvent('onclose',function (this, data)
    --     inst.AnimState:SetSymbolLightOverride('thumbnail',0)
    --     inst.AnimState:PlayAnimation('close',true)
    --     inst.components.inventoryitem:ChangeImageName("webbert_pack")
    -- end)

    inst:ListenForEvent('itemget',
    ---comment
    ---@param this ent
    ---@param data event_data_itemget
    function (this, data)
        if not inst.webbert_seedbox_keepitem then
            local item = data and data.item
            if item and TUNING.MOD_WEBBER_THE_TRAINER.map_seeds[item.prefab] then
                local num = item.components.stackable and item.components.stackable:StackSize() or 1
                inst.components.webbert_seedbox:GetSeeds(item.prefab,num)
                inst:DoTaskInTime(0,function ()
                    -- inst.components.container:DestroyContents()
                    item:Remove()
                end)
            end
        end
    end)

    -- inst.OnSave = onsave
    -- inst.OnPreLoad = onpreload

    return inst
end

return Prefab("common/inventory/"..prefab_id, fn, assets, prefabs)